<?php
include APPLICATION_PATH . '/application/views/head.phtml';
include APPLICATION_PATH . '/application/views/top.phtml';
include APPLICATION_PATH . '/application/views/left.phtml';
?>
<!-- BEGIN PAGE -->
<script src="/public/js/jquery-migrate-1.2.1.min.js" type="text/javascript"></script>
<script >
$(document).ready(function() {

$("#uname").blur(function(){
	
	var myReg = /^[\u4e00-\u9fa5-a-zA-Z]+$/;
               //var myReg = /^[\u4e00-\u9fa5]+$/;
                if (myReg.test($("input[type=text]").val())) {
                    //alert("输入正确");
                } else {
                    layer.alert("用户真实名为汉字或字母");
                    document.getElementById('uname').value="";
                }
       })
$("#truename").blur(function(){
               var regExp = /[a-zA-Z]$/;
               if(!regExp.test($(this).val())){
                      $(this).val("");
               }
              
       })

	var _userForm = $('#form_user_add');
	var _editForm = $('#form_user_edit');

	$('#submit_form').click(function() {
			
		var objUserForm = {
			_name: $("input[name='name']", _userForm),
			_rname: $("input[name='realname']", _userForm),
			_group: $("#group"),
			_password: $("input[name='password']", _userForm),
			_password2: $("input[name='password2']", _userForm)

		};
		var pattern = new RegExp("[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]");
		for (var i in objUserForm) {
			if (objUserForm[i].val() == '' || objUserForm[i].val() == null || pattern.test(objUserForm[i].val())) {
				objUserForm[i].css({
					'border-color': 'red'
				});
				layer.tips('必填项（请勿输入特殊字符）', objUserForm[i]);
				return false;
			} else {
				objUserForm[i].css({
					'border-color': '#e5e5e5'
				});
			}
		}

		if(objUserForm._password.val().length<6){
			layer.alert('密码必须为6位以上！');
			return false;
		}
		if (objUserForm._password.val() != objUserForm._password2.val()) {
			layer.alert('两次输入的密码必须一致！');
			return false;
		}

		$.ajax({
			type: "POST",
			url: "/User/add",
			dataType: "JSON",
			async: true,
			data: {
				"realname": objUserForm._name.val(),
				"name": 　objUserForm._rname.val(),
				"group_id": objUserForm._group.val(),
				"password": objUserForm._password.val()

			},
			success: function(data) {
				layer.alert(data.info, function(index) {
					if (true === data.status){
						window.location = "/User/list";
					}else if(false === data.status){
						layer.close(index);
					}
				});

			},
			error: function(e) {

				console.log(e);
			}


		});
	});

	/*function getPageAndLocation(){
		$.ajax({
			type: 'POST',
			url: '/User/page/page/' + urlId,
			data: urlId,
			success: function(data) {
				var data = $.parseJSON(data);

				$('#tbody').html('');
				var id = '';


				$.each(data, function(k, v) {
					//console.log('k='+k);
					id = v['user_id'];


					$('#tbody').append($('<tr class="gradeX"><td>' + v['username'] + '</td><td>' + v['realname'] + '</td><td>' + v['group']['name'] + '</td><td style="display:none;">' + v['password'] + '</td><td>' + v['current_time'] + '</td><td group_id="' + v['group_id'] + '" item_id="' + id + '"><a  class="tableDelete" href="/User/del/id/' + id + '">删除</a> / <a class="tableEdit" href="/User/edit/id/' + id + '">修改</a> </td></tr>'));

				});

			}

		});
		$.ajax({
			type: 'POST',
			url: '/User/currentPage/page/' + urlId,
			data: urlId,
			success: function(data) {
				console.log(data);
				$('#pages').html(data);
				$('#pages').ready(function() {
					nextBtn = $('#page_next');
				});
			}

		});
		return false;
	}*/



	//$('.userDelete').click(function(){
	$(document).on('click', '.tableDelete', function() {
		//layer.ready(function(){
		var userId = $(this).parent('td').attr('item_id'),
			thisUrl = $(this).attr('href');
		//官网欢迎页   
		layer.confirm('确认删除', {
			btn: ['确认', '取消'] //按钮
		}, function(index) {

			$.ajax({
				type: "GET",
				url: thisUrl,
				async: true,
				data: {
					//"id" : userId
				},
				success: function(data) {
					layer.alert(data.info, function(index) {
						if (true === data.status){
							window.location = "/User/list";
						}
					});
				}
			});
			layer.close(index);
			return false;

		}, function(index) {
			layer.close(index);
		});
		return false;

	});

	//$('.userEdit').click(function(){
	$(document).on('click', '.tableEdit', function(e) {
		$('#modal_edit').modal('toggle');
		var _parent = $(this).parent('td').parent('tr.gradeX');
		userId = $(this).parent('td').attr('item_id');
		groupId = $(this).parent('td').attr('group_id');
		objEditForm = {
			_name: $("input[name='name']", _editForm),
			_realName: $("input[name='realname']", _editForm),
			_groupId: $('#groupEdit', _editForm),
			//_password: $("input[name='password']", _editForm),
			_password2: $("input[name='password2']", _editForm),
			_password3: $("input[name='password3']", _editForm)

		};

		var tdTable = [];
		$('td', _parent).each(function() {
			tdTable.push($(this).text());
		});
<?php 
foreach ($userList as $k => $v) {
	echo 'var vv=' . json_encode($v['username'], true) . ';';
}
//var_dump($userList['password']);
echo 'var group_authority_list=' . json_encode($userList[0]['password'], true) . ';'; ?>
		objEditForm._name.val(tdTable[0]);
		objEditForm._realName.val(tdTable[1]);
		//objEditForm._password.val(tdTable[3]);
		
//var a= vv;
//alert(vv);		
//var obj = group_authority_list;
//alert(obj);
//objEditForm._password.val(obj);
		$('option', objEditForm._groupId).each(function() {
			if ($(this).val() == groupId) {
				$(this).attr('selected', true);
			}
		});

		return false;
	});

	$('#submitEdit').click(function() {
		for (var i in objEditForm) {
			if (objEditForm[i].val() == '' || objEditForm[i].val() == null) {
				objEditForm[i].css({
					'border-color': 'red'
				});
				return false;
			} else {
				objEditForm[i].css({
					'border-color': '#e5e5e5'
				});
			}
		}
		if(objEditForm._password2.val().length<6){
			layer.alert('密码必须为6位以上！');
			return false;
		}
		if (objEditForm._password2.val() != objEditForm._password3.val()) {
			layer.alert('两次输入的密码必须一致！');
			return false;
		}
		$.ajax({
			type: "POST",
			url: "/User/edit/id/" + userId,
			async: true,
			data: {
				"name": objEditForm._name.val(),
				"realname": 　objEditForm._realName.val(),
				"group_id": objEditForm._groupId.val(),
				//"password": objEditForm._password.val(),
				"password2": objEditForm._password2.val()

			},
			success: function(data) {
				layer.msg(data.info);
				if (true === data.status) {
					window.location = "/User/list";
					//getPageAndLocation();
				}


			}
		});


	});


	//var nextBtn = $('#page_next');
	$(document).on('click', '#page_next,#page_prev,#page_index,#page_end', function() {
		var urlOld = $(this).attr('href'),
			urlArr = urlOld.split("/");
		urlId = urlArr[urlArr.length - 1];
		$.ajax({
			type: 'POST',
			url: '/User/page/page/' + urlId,
			data: urlId,
			success: function(data) {
				var data = $.parseJSON(data);

				$('#tbody').html('');
				var id = '';


				$.each(data, function(k, v) {
					//console.log('k='+k);
					id = v['user_id'];

					$('#tbody').append($('<tr class="gradeX"><td>' + v['username'] + '</td><td>' + v['realname'] + '</td><td>' + v['group_name'] + '</td><td style="display:none;">' + v['password'] + '</td><td>' + v['current_time'] + '</td><td group_id="' + v['group_id'] + '" item_id="' + id + '"><a  class="tableDelete" href="/User/del/id/' + id + '">删除</a> / <a class="tableEdit" href="/User/edit/id/' + id + '">修改</a> </td></tr>'));

				});

			}

		});
		$.ajax({
			type: 'POST',
			url: '/User/currentPage/page/' + urlId,
			data: urlId,
			success: function(data) {
				console.log(data);
				$('#pages').html(data);
				$('#pages').ready(function() {
					nextBtn = $('#page_next');
				});
			}

		});
		return false;
	});



});
</script>

<div class="page-content">

	<!-- BEGIN SAMPLE PORTLET CONFIGURATION MODAL FORM-->

	<div id="portlet-config" class="modal hide">

		<div class="modal-header">

			<button data-dismiss="modal" class="close" type="button"></button>

			<h3>portlet Settings</h3>

		</div>

		<div class="modal-body">

			<p>Here will be a configuration form</p>

		</div>

	</div>

	<!-- END SAMPLE PORTLET CONFIGURATION MODAL FORM-->

	<!-- BEGIN PAGE CONTAINER-->

	<div class="container-fluid">

		<!-- BEGIN PAGE HEADER-->

		<div class="row-fluid">

			<div class="span12">

				<!-- BEGIN STYLE CUSTOMIZER -->

				<div class="color-panel hidden-phone">

					<!-- <div class="color-mode-icons icon-color"></div> -->

					<div class="color-mode-icons icon-color-close"></div>

					<div class="color-mode">

						<p>THEME COLOR</p>

						<ul class="inline">

							<li class="color-black current color-default" data-style="default"></li>

							<li class="color-blue" data-style="blue"></li>

							<li class="color-brown" data-style="brown"></li>

							<li class="color-purple" data-style="purple"></li>

							<li class="color-grey" data-style="grey"></li>

							<li class="color-white color-light" data-style="light"></li>

						</ul>

						<label>

							<span>Layout</span>

							<select class="layout-option m-wrap small">

								<option value="fluid" selected>Fluid</option>

								<option value="boxed">Boxed</option>

							</select>

						</label>

						<label>

							<span>Header</span>

							<select class="header-option m-wrap small">

								<option value="fixed" selected>Fixed</option>

								<option value="default">Default</option>

							</select>

						</label>

						<label>

							<span>Sidebar</span>

							<select class="sidebar-option m-wrap small">

								<option value="fixed">Fixed</option>

								<option value="default" selected>Default</option>

							</select>

						</label>

						<label>

							<span>Footer</span>

							<select class="footer-option m-wrap small">

								<option value="fixed">Fixed</option>

								<option value="default" selected>Default</option>

							</select>

						</label>

					</div>

				</div>

				<!-- END BEGIN STYLE CUSTOMIZER -->

				<!-- BEGIN PAGE TITLE & BREADCRUMB-->

				<h3 class="page-title">

					用户列表

				</h3>

				<ul class="breadcrumb">

					<li>

						<i class="icon-home"></i>
						<span>当前位置：</span>
						<span>首页</span>

						<i class="icon-angle-right"></i>

					</li>

					<li>

						<span>用户管理</span>

						<i class="icon-angle-right"></i>

					</li>

					<li><span>用户列表</span></li>

				</ul>

				<!-- END PAGE TITLE & BREADCRUMB-->

			</div>

		</div>

		<!-- END PAGE HEADER-->

		<!-- BEGIN PAGE CONTENT-->

		<div class="row-fluid">

			<div class="span6 responsive" data-tablet="span12 fix-offset" data-desktop="span6" style="width:100%;">

				<!-- BEGIN EXAMPLE TABLE PORTLET-->

				<div class="portlet box grey">

					<div class="portlet-title">

						<div class="caption"><i class="icon-user"></i>用户列表</div>

						<div class="actions controls">

							<a href="#modal_add" data-toggle="modal" data-target="#modal_add" class="btn blue"><i class="icon-pencil"></i> 添加</a>
							<!--<a href="/Index/Authority/add" class="btn blue"><i class="icon-pencil"></i> 添加</a>-->

						</div>

					</div>

					<div class="portlet-body">

						<table class="table table-striped table-bordered table-hover" id="current_table">

							<thead>

							<tr>
								<th>用户名</th>
								<th>真实姓名</th>
								<th>用户组</th>
								<th style="display:none;">用户密码</th>
								<th>加入时间</th>
								<th>操作</th>
							</tr>

							</thead>

							<tbody id="tbody">
							<?php
						
							foreach ($userList as $k => $v) {
								
								$style = (0 == ($k % 2)) ? 'odd' : 'even';
								echo <<< EOF
										<tr class="{$style} gradeX">

											<td>{$v['username']}</td>
											<td>{$v['realname']}</td>
											<td>{$v['group_name']}</td>
											<td style="display:none;">{$v['password']}</td>
											<td>{$v['current_time']}</td>
											<td item_id="{$v['user_id']}" group_id="{$v['group_id']}"><a class="tableDelete" href="/User/del/id/{$v['user_id']}">删除</a> / <a class="tableEdit" href="/User/edit/id/{$v['user_id']}">修改</a></td>
										</tr>
EOF;
							}
							?>
							</tbody>
						</table>
                <div id="pages">
<?php
    if($userList[0]['rows']/$userList[0]['every_page']<=1)
        {
            
        }
        else
        {
            print_r($allpage);
        }
?>
</div>
					</div>

				</div>

				<div id="modal_add" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel3" aria-hidden="true">

					<div class="modal-header">

						<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>

						<h3 id="myModalLabel3">添加用户</h3>

					</div>

					<div class="modal-body">

						<form action="#" id="form_user_add" method="post" class="form-horizontal">

							<div class="control-group">

								<label class="control-label">真实姓名</label>

								<div class="controls">

									<input type="text" class="colorpicker-default m-wrap" name="name" value="" id="uname">

								</div>

							</div>

							<div class="control-group">

								<label class="control-label">用户名</label>

								<div class="controls">

									<input type="text" class="colorpicker-default m-wrap" name="realname" value="" id="truename">

								</div>

							</div>

							<div class="control-group">

								<label class="control-label">分组</label>

								<div class="controls">

									<select id="group" name="group_id">
										<option>--请选择--</option>
										<?php
										foreach ($groupList AS $k => $v) {
											echo '<option value="' . $v['group_id'] . '">' . $v['name'] . '</option>';
										}
										?>
									</select>

								</div>

							</div>

							<div class="control-group">

								<label class="control-label">密码</label>

								<div class="controls">

									<input type="password" class="colorpicker-default m-wrap" name="password" value="">

								</div>

							</div>

							<div class="control-group">

								<label class="control-label">确认密码</label>

								<div class="controls">

									<input type="password" class="colorpicker-default m-wrap" name="password2" value="">

								</div>

							</div>
							

						</form>

					</div>

					<div class="modal-footer">

						<button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>

						<button class="btn green btn-primary" id="submit_form" data-dismiss="modal" >提交</button>

					</div>

				</div>

				<div id="modal_edit" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel3" aria-hidden="true">

					<div class="modal-header">

						<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>

						<h3 id="myModalLabel3">修改用户资料</h3>

					</div>

					<div class="modal-body">

						<form action="#" id="form_user_edit" method="post" class="form-horizontal">

							<div class="control-group">

								<label class="control-label">用户名</label>

								<div class="controls">

									<input type="text" disabled="disabled" class="colorpicker-default m-wrap" name="name" value="">

								</div>

							</div>

							<div class="control-group">

								<label class="control-label">真实姓名</label>

								<div class="controls">

									<input type="text" class="colorpicker-default m-wrap" name="realname" value="" id="realname" onblur="value=value.replace(/[\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[\d]/g,''))">

								</div>

							</div>

							<div class="control-group">

								<label class="control-label">分组</label>

								<div class="controls">

									<select id="groupEdit" name="group">

										<?php

										foreach ($groupList AS $k => $v) {
											echo '<option value="' . $v['group_id'] . '">' . $v['name'] . '</option>';
										}
										?>
									</select>

								</div>

							</div>

							<!-- <div class="control-group">

								<label class="control-label">原密码</label>

								<div class="controls">

									<input type="password" class="colorpicker-default m-wrap" name="password" value="">

								</div>

							</div> -->

							<div class="control-group">

								<label class="control-label">新密码</label>

								<div class="controls">

									<input type="password" class="colorpicker-default m-wrap" name="password2" value="">

								</div>

							</div>

							<div class="control-group">

								<label class="control-label">确认密码</label>

								<div class="controls">

									<input type="password" class="colorpicker-default m-wrap" name="password3" value="">

								</div>

							</div>

							

						</form>

					</div>

					<div class="modal-footer">

						<button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>

						<button class="btn green btn-primary" id="submitEdit" data-dismiss="modal" >提交</button>

					</div>

				</div>

				<!-- END EXAMPLE TABLE PORTLET-->

			</div>

		</div>

		<!-- END PAGE CONTENT-->

	</div>

	<!-- END PAGE CONTAINER-->

</div>
<div id="thispage" data-id="21"></div>
<!-- END PAGE -->
<?php
include APPLICATION_PATH . '/application/views/foot.phtml';
?>
